package com.example.es.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

/**
 * @description:  terms query
 * @author: ruoan
 * @date: 2020/10/11 11:57
 */
public class EsRestHighLevelClientTest7 {

    private RestHighLevelClient client= EsRestHighLevelClient.getClient();
    ObjectMapper mapper = new ObjectMapper();
    String index = "sms-logs-index";
    String type="sms-logs-type";

    @Test
    public void terms() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(index).types(type);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(4);
        searchSourceBuilder.query(QueryBuilders.termsQuery("province", "北京", "晋城"));
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }


    }







}